package downloads;

import com.icesoft.faces.context.FileResource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import lecture.Lecture;
import lecture.LectureManagedBean;
import org.icefaces.component.fileentry.FileEntry;
import org.icefaces.component.fileentry.FileEntryEvent;
import org.icefaces.component.fileentry.FileEntryResults;
import org.icefaces.component.fileentry.FileEntryResults.FileInfo;

/**
 * 
 * @author Thorsten Ludwig
 */
@ManagedBean(name="downloadPageBean")
@CustomScoped(value="#{window}")
public class DownloadPageManagedBean {
    
    private String newCategoryName;
    
    private String newDownloadName;
    
    private DownloadCategory newDownloadCategory;
    
    private boolean isExternalLink;
    
    private String externalLink;
    
    private FileResource resource;
    
    private String filename;
    
    public DownloadPageManagedBean() {
        
    }

    public String getExternalLink() {
        return externalLink;
    }

    public void setExternalLink(String externalLink) {
        this.externalLink = externalLink;
    }

    public boolean isIsExternalLink() {
        return isExternalLink;
    }

    public void setIsExternalLink(boolean isExternalLink) {
        this.isExternalLink = isExternalLink;
    }

    public String getNewCategoryName() {
        return newCategoryName;
    }

    public void setNewCategoryName(String newCategoryName) {
        this.newCategoryName = newCategoryName;
    }

    public DownloadCategory getNewDownloadCategory() {
        return newDownloadCategory;
    }

    public void setNewDownloadCategory(DownloadCategory newDownloadCategory) {
        this.newDownloadCategory = newDownloadCategory;
    }

    public String getNewDownloadName() {
        return newDownloadName;
    }

    public void setNewDownloadName(String newDownloadName) {
        this.newDownloadName = newDownloadName;
    }

    public FileResource getResource() {
        return resource;
    }

    public void setResource(FileResource resource) {
        this.resource = resource;
    }
    
    public void uploadListener(FileEntryEvent event) {
        FileEntry fileEntry = (FileEntry) event.getSource();
        FileEntryResults results = fileEntry.getResults();
        FileResource fileResource;
        for (FileInfo fileInfo : results.getFiles()) {
            if (fileInfo.isSaved()) {
                fileResource = new FileResource(fileInfo.getFile());
                this.filename = fileInfo.getFileName();
                this.resource = fileResource;
            }
        }
    }
    
    public void addDownload() {
        Download d = new Download();
        if (d.isIsExternalLink()) {
            d.setIsExternalLink(this.isExternalLink);
            d.setExternalLink(this.externalLink);
        } else {
            d.setResource(this.resource.getFile());
            d.setFilename(this.filename);
        }
        d.setName(this.newDownloadName);
        this.newDownloadCategory.addDownload(d);
    }
    
    public List<SelectItem> getDownloadItems() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture l = lb.getLecture();
        List<SelectItem> list = new ArrayList<SelectItem>();
        for (DownloadCategory dc: l.getDownloads()) {
            list.add(new SelectItem(dc, dc.getName()));
        }
        return list;
    }
    
    public FileResource getResource(File file) {
        if (file == null)
            return null;
        else
            return new FileResource(file);
    }
}
